import request from "./request";
import getUrlParameter from "@/utils/urlparameter";
import { ElMessage, ElMessageBox } from "element-plus";
import type { Action } from "element-plus";
import type { AnyColumns } from "element-plus/lib/components/table-v2/src/types.js";
import { useStore } from "@/stores";

/**
 * 通道操作接口地址
 */
const aisleHandle: any = {
    DO4: "/modbus-api/deviceOperate/sendControlByMygy", // 母液供应
    DO5: "/modbus-api/deviceOperate/sendControlByMygy", // 空气泵启动
    DO6: "/modbus-api/deviceOperate/sendControlByMygy", // 高压空气开关
    DO7: "/modbus-api/deviceOperate/sendControlByMygy", // 低压空气开关
    DO8: "/modbus-api/deviceOperate/sendControlByMygy", // 高压泵启动
    DO9: "/modbus-api/deviceOperate/sendControlByMygy", // 高压泄压开关
    DO11: "/modbus-api/deviceOperate/sendControlByMygy", // 滴灌泄压开关
    DO12: "/modbus-api/deviceOperate/sendControlByMygy", // 砂石过滤器排水
};
// 执行开关操作
const handle = (item: any, callback: Function) => {
    // 打开设备远程控制
    longDistance("1", () => {
        //  电源操作
        powerLight("1", (long: any) => {
            if (long) {
                const name = item.name;
                switch (name) {
                    case "电源总闸":
                        callback(true);
                        break;
                    case "清水供应":
                        alterTheState(item.value, "1", () => {
                            callback(true);
                        });
                        break;
                    case "肥水供应":
                        alterTheState(item.value, "1", () => {
                            callback(true);
                        });
                        break;
                    case "母液供应":
                        openOnOff(item.value, "DO4", () => {
                            callback(true);
                        });
                        break;
                    case "空气泵启动":
                        openOnOff(item.value, "DO5", () => {
                            callback(true);
                        });
                        break;
                    case "高压空气开关":
                        openOnOff(item.value, "DO6", () => {
                            callback(true);
                        });
                        break;
                    case "低压空气开关":
                        openOnOff(item.value, "DO7", () => {
                            callback(true);
                        });
                        break;
                    case "高压泵启动":
                        openOnOff(item.value, "DO8", () => {
                            callback(true);
                        });
                        break;
                    case "高压泄压开关":
                        openOnOff(item.value, "DO9", () => {
                            callback(true);
                        });
                        break;
                    case "滴灌泵启动":
                        pump(item.value, () => {
                            callback(true);
                        });

                        break;
                    case "滴灌泄压开关":
                        openOnOff(item.value, "DO11", () => {
                            callback(true);
                        });
                        break;
                    case "砂石过滤器排水":
                        openOnOff(item.value, "DO12", () => {
                            callback(true);
                        });
                        break;
                    default:
                }
            }
        });
    });
};

// 判断开关是否全部关闭
const judge = (callback: Function) => {
    // 开关未全部关闭
    // if (!list.includes("1")) {
    powerLight("0", () => {
        longDistance("0", () => {
            callback();
        });
    });

    // }
};
/**
 * 切换机房远程或本地的操作
 * 0关，1打开
 */
const getIoState = () => {
    const store = useStore();
    let data = {
        msg: store.getState,
    };
    return new Promise((resolve, reject) => {
        resolve(data);
    });
};
const longDistance = (data: any, callback: Function) => {
    // 查询机房设备远程状态
    // request.post(`/modbus-api/deviceOperate/getDiByUidIo1?addr=${getUrlParameter("id")}`).then((res: any) => {
    getIoState().then((res: any) => {
        console.log(res, "新获取状态");
        // 如果远程为关闭状态，则打开远程控制
        if ((res.msg == 0 && data == 1) || (res.msg == 1 && data == 0)) {
            request.post(`/modbus-api/deviceOperate/sendControlByQhycz?status=${data}&jfDeviceId=${getUrlParameter("id")}`).then((re: any) => {
                if (re.msg == "操作成功") {
                    console.log(`设备远程${data == "1" ? "开启" : "关闭"}成功`);
                    callback(true);
                } else {
                    ElMessageBox.alert("机房设备切换远程操作失败，请稍后再试！", "提示", {
                        confirmButtonText: "确定",
                    });
                }
            });
        } else {
            callback();
        }
    });
};
/**
 * 电源总闸合闸操作
 */
const powerLight = (onOffType: any, callback: Function) => {
    request.post(`/modbus-api/deviceOperate/getDiByUidIo0?addr=${getUrlParameter("id")}`).then((res: any) => {
        console.log(`电源总闸状态：${res.msg == "1" ? "开" : "关"}`);
        // 如果没有合闸，，，则合闸，
        if ((onOffType == 1 && res.msg == 0) || (onOffType == 0 && res.msg == 1)) {
            request.post(`/modbus-api/deviceOperate/sendControlByDDyzz?status=${onOffType}&jfDeviceId=${getUrlParameter("id")}`).then((re: any) => {
                if (re.msg == "操作成功") {
                    console.log(`电源${onOffType == "1" ? "合闸" : "断开"}成功`);
                    callback(true);
                } else {
                    ElMessageBox.alert("电源操作失败，请稍后再试！", "提示", {
                        confirmButtonText: "确定",
                    });
                }
            });
        } else {
            callback(true);
        }
        // 断电
        // if (onOffType == 0 && res.msg == 1) {
        //     request.post(`/modbus-api/deviceOperate/sendControlByDDyzz?status=0&jfDeviceId=${getUrlParameter("id")}`).then((re: any) => {
        //         if (re.msg == "操作成功") {
        //             console.log("电源断开成功");
        //             callback(true);
        //         } else {
        //             ElMessageBox.alert("电源断开失败，请稍后再试！", "提示", {
        //                 confirmButtonText: "确定",
        //             });
        //             callback(false);
        //         }
        //     });
        // } else if (onOffType == 0 && res.msg == 0) {
        //     callback(true);
        // }
    });
};
/**
 * 修改清肥水供应状态
 */
const alterTheState = (data: any, type: any, callback: Function) => {
    // 查询供应状态
    // 1清水 2肥水
    request.post(`/modbus-api/deviceOperate/getDiByUidQsfs?addr=${getUrlParameter("id")}&type=${type}`).then((res: any) => {
        console.log(`${type == 1 ? "清水" : "肥水"}供应状态：${res.msg}`);
        if ((data == 1 && res.msg == 0) || (data == 0 && res.msg == 1)) {
            // 切换供应状态
            request.post(`/modbus-api/deviceOperate/sendControlByqsfsgy?status=${data}&jfDeviceId=${getUrlParameter("id")}&type=${type}`).then((re: any) => {
                if (re.msg == "操作成功") {
                    console.log(`${type == 1 ? "清水" : "肥水"}供应设置成功`);
                    callback(true);
                } else {
                    ElMessageBox.alert("供应状态切换失败，请稍后再试！", "提示", {
                        confirmButtonText: "确定",
                    });
                }
            });
        } else {
            callback(true);
        }
        // if (data == 0 && res.msg == 1) {
        //     // 切换供应状态
        //     request.post(`/modbus-api/deviceOperate/sendControlByqsfsgy?status=0&jfDeviceId=${getUrlParameter("id")}&type=${type}`).then((re: any) => {
        //         if (re.msg == "操作成功") {
        //             console.log(`${type == 1 ? "清水" : "肥水"}供应设置成功`);
        //         } else {
        //             ElMessageBox.alert("供应状态切换失败，请稍后再试！", "提示", {
        //                 confirmButtonText: "确定",
        //             });
        //         }
        //     });
        // } else if (data == 0 && res.msg == 0) {
        //     callback(true);
        // }
    });
};
/**
 * 获取不同通道状态
 */
const aisle = (aisleNum: any, callback: Function) => {
    request.get(`/modbus-api/deviceOperate/getDiByUidAndOperate?addr=${getUrlParameter("id")}&operate=${aisleNum}`).then((res: any) => {
        console.log(res);
        if (res.msg == "1") {
            callback(true);
        } else {
            callback(false);
        }
    });
};
/**
 * 开启通道开关
 */
const openOnOff = (val: any, aisleNum: any, callback: Function) => {
    let url = `${aisleHandle[aisleNum]}?status=${val}&jfDeviceId=${getUrlParameter("id")}`;
    aisle(aisleNum, (open: any) => {
        // true 状态为打开
        if ((!open && val == 1) || (open && val == 0)) {
            // console.log(url);
            request.post(url).then((res: any) => {
                if (res.msg == "操作成功") {
                    console.log(`通道${val == "1" ? "打开" : "关闭"}成功`);
                    callback(true);
                } else {
                    ElMessageBox.alert("操作失败，请稍后再试！", "提示", {
                        confirmButtonText: "确定",
                    });
                }
            });
        } else {
            callback(true);
        }
    });
};
/**
 * 滴灌泵启动
 */
const pump = (val: any, callback: Function) => {
    // 查询滴灌泵状态
    request.post(`/modbus-api/deviceOperate/getDiByUid?addr=${getUrlParameter("id")}`).then((res: any) => {
        let data = res.msg;
        if ((val == 1 && data == 0) || (val == 0 && data == 1)) {
            request.post(`/modbus-api/deviceOperate/sendControlByDgb?status=${val}&jfDeviceId=${getUrlParameter("id")}`).then((res: any) => {
                if (res.msg == "操作成功") {
                    console.log(`水泵${val == "1" ? "打开" : "关闭"}成功`);
                    callback(true);
                } else {
                    ElMessageBox.alert("水泵操作失败，请稍后再试！", "提示", {
                        confirmButtonText: "确定",
                    });
                }
            });
        } else {
            callback(true);
        }
    });
};
export { handle, judge };
